package com.yutianran.springBoot.spider;

import com.alibaba.fastjson.JSON;
import com.yutianran.springBoot.spider.CookiesBean;
import com.yutianran.springBoot.util.FileUtil;
import io.reactivex.functions.Function;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取Cookies
 * src:Cookies在resources目录下的文件路径
 * dest:Headers的Map
 */
public class CookiesFunction implements Function<String, Map<String, String>> {

    @Override
    public Map<String, String> apply(String pathFile) throws Exception {
        String path = getClass().getResource(pathFile).getPath();
        String string = FileUtil.readBytesToString(new File(path));
        List<CookiesBean> list = JSON.parseArray(string, CookiesBean.class);
        Map<String, String> map = new HashMap<String, String>();
        for (CookiesBean cookie : list) {
            map.put(cookie.getName(), cookie.getValue());
        }
        return map;
    }
}
